package l3s.wikirevisions;
    
import java.util.ArrayList;
import java.util.List;

import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.StreamingResolution;

import org.apache.commons.codec.binary.Base64;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
    
public class VisualizationActionBean implements ActionBean{
	
	private static final int NUM_ROWS = 100;
    
	// input fields:	
	private String page_ID;
	private String query;

    private ActionBeanContext context;
    private static SolrServer solrServer;
    private List<Result> results;
    
    //sorted by date
    private List<Result> sortedResults;
    
    private int numFound;
    
    
	@Override
	public ActionBeanContext getContext() {
		// TODO Auto-generated method stub
		return context;
	}	

	public String getPage_ID() {
		return page_ID;
	}

	public int getNumFound() {
		return numFound;
	}

	public void setNumFound(int numFound) {
		this.numFound = numFound;
	}

	public void setPage_ID(String page_ID) {
		this.page_ID = page_ID;
	}

	
	
	public List<Result> getSortedResults() {
		return sortedResults;
	}

	public void setSortedResults(List<Result> sortedResults) {
		this.sortedResults = sortedResults;
	}

	public List<Result> getResults() {
		return results;
	}

	public String getQuery() {
		return query;
	}

	public void setQuery(String query) {
		this.query = query;
	}

	public void setResults(List<Result> results) {
		this.results = results;
	}
	
	public static boolean less_than(Result r1, Result r2){
		return (r1.getRevision().compareTo(r2.getRevision()) < 0);
	}

	public void sortResults(){
		sortedResults = new ArrayList();
		for (Result r: results)
			sortedResults.add(r);
		
		for (int i = 0; i < sortedResults.size() - 1; i++)
			for (int j = 0; j < sortedResults.size(); j++)
				if (less_than(sortedResults.get(j), sortedResults.get(i))){
					Result tempj = sortedResults.get(j);
					Result temp = sortedResults.remove(i);
					sortedResults.set(i, tempj);
					sortedResults.remove(j);
					sortedResults.set(j, temp);
				}
	}

	@Override
	public void setContext(ActionBeanContext arg0) {
		// TODO Auto-generated method stub
		this.context = arg0;
	}

	@DefaultHandler
	public Resolution visualize(){
		solrServer = Utils.getSolrServerForWikiPages();
		System.out.println(solrServer);
		QueryResponse qresponse = null;
		
		if (solrServer != null){
        	SolrQuery solrQuery = new SolrQuery();
        	solrQuery.setRows(NUM_ROWS);
        	solrQuery.setQuery(query);
        	
        	solrQuery.addFilterQuery("page_ID:" + page_ID);
        	solrQuery.setFields("ID, page_ID, revision, page_revision, original_text, entities, score");

        	//For highlighting
        	solrQuery.setHighlight(true);
        	solrQuery.setHighlightSimplePre("<strong>");
        	solrQuery.setHighlightSimplePost("</strong>");
        	solrQuery.setHighlightFragsize(100);
        	solrQuery.setHighlightSnippets(10);
        	
        	try {
				qresponse = solrServer.query(solrQuery);
				System.out.println(qresponse.getHighlighting());
			} catch (SolrServerException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}			
		}

		System.out.println("QRespone: " + qresponse);
		if (qresponse != null){
			results = qresponse.getBeans(Result.class);
			if (qresponse.getHighlighting() != null)
				for (Result r: results)
					r.setSnippets(qresponse.getHighlighting().get(r.getID()).get("text"));
			numFound = (int)qresponse.getResults().getNumFound();
		}		
		
		System.out.println("Results: " + results.size());
		return new ForwardResolution("/visualization.jsp");		
	}
}
